package fr.webviewer.data;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.graphics.Point;

/**
 * Contains all the road entries
 * @author Anthony
 *
 */
public class RoadEntryPool {

	private static RoadEntryPool instance;
	
	public static RoadEntryPool getInstance(){
		if(instance == null){
			instance = new RoadEntryPool();
		}
		return instance;
	}
	
	private Map<String, RoadEntry> entryMap;
	
	private List<String> labelList;
	
	public RoadEntryPool(){
		entryMap = new HashMap<String, RoadEntry>();
		labelList = new ArrayList<String>();
		add("1", new RoadEntry("1", new Point(454, 75)));
		add("2",new RoadEntry("2", new Point(439, 74)));
		add("3",new RoadEntry("3", new Point(418, 86)));
		RoadEntry entry4 = new RoadEntry("4", new Point(345, 70));
		RoadEntry entry4a = new RoadEntry("4a", new Point(296, 57));
		RoadEntry entry4b = new RoadEntry("4b", new Point(324, 63));
		entry4.add(entry4);
		entry4.add(entry4a);
		entry4.add(entry4b);		
		add("4", entry4);
		add("4a", entry4a);
		add("4b", entry4b);
		add("5",new RoadEntry("5", new Point(269, 53)));
		add("6",new RoadEntry("6", new Point(238, 51)));
		add("7", new RoadEntry("7", new Point(212, 76)));
		add("8",new RoadEntry("8", new Point(172, 106)));
		add("9",new RoadEntry("9", new Point(135, 139)));
		add("10",new RoadEntry("10", new Point(113, 179)));
		RoadEntry entry11 = new RoadEntry("11");
		RoadEntry entry11a = new RoadEntry("11a", new Point(104, 199));
		RoadEntry entry11b = new RoadEntry("11b", new Point(102, 212));
		entry11.add(entry11a);
		entry11.add(entry11b);
		add("11", entry11);
		add("11a", entry11a);
		add("11b", entry11b);
		add("12",new RoadEntry("12", new Point(105, 247)));
		add("13",new RoadEntry("13", new Point(130, 294)));
		add("14",new RoadEntry("14", new Point(155, 326)));
		add("15",new RoadEntry("15", new Point(210, 340)));
		add("16",new RoadEntry("16", new Point(244, 340)));
		add("17",new RoadEntry("17", new Point(276, 355)));
		RoadEntry entry18 = new RoadEntry("18");
		RoadEntry entry18a = new RoadEntry("18a", new Point(310, 370));
		RoadEntry entry18b = new RoadEntry("18b", new Point(327, 374));
		entry18.add(entry18a);
		entry18.add(entry18b);
		add("18", entry18);
		add("18a", entry18a);
		add("18b",entry18b);
		add("19",new RoadEntry("19", new Point(360, 372)));
		add("20",new RoadEntry("20", new Point(381, 335)));
		add("21",new RoadEntry("21", new Point(397, 304)));
		add("22",new RoadEntry("22",new Point( 420, 287)));
		add("23",new RoadEntry("23", new Point(437, 252)));
		add("24",new RoadEntry("24", new Point(465, 192)));
		add("25",new RoadEntry("25", new Point(462, 161)));
		add("26",new RoadEntry("26", new Point(462, 123)));
	}
	
	private void add(String label, RoadEntry entry){
		entryMap.put(label, entry);
		if(entry.getPosition() != null){
			labelList.add(label);
		}
	}

	public List<RoadEntry> getRoadBetween(String firstLabel, String secondLabel){
		List<RoadEntry> result = new ArrayList<RoadEntry>();
		int firstId = labelList.indexOf(firstLabel);
		int secondId = labelList.indexOf(secondLabel);
		if(firstId == -1){
			firstId = 0;
		} 
		if(secondId == -1){
			secondId = 0;
		}
		if(firstId == secondId){
			return result;
		}
		if(firstId > secondId){
			for(int i = firstId; i < labelList.size(); i++){
				result.add(entryMap.get(labelList.get(i)));
			}
			for(int i = 0; i <= secondId; i++){
				result.add(entryMap.get(labelList.get(i)));
			}
			
		}		
		for(int i = firstId; i <= secondId; i++){
			result.add(entryMap.get(labelList.get(i)));
		}
		return result;
	}
	
	public boolean contains(String label){
		return entryMap.containsKey(label);
	}
	
	public RoadEntry getRoadEntry(String label){
		if(!this.contains(label)){
			return entryMap.get("1");
		}
		return entryMap.get(label);
	}
	
		
}
